#coding=utf-8
#Boa:Dialog:DlgEditorSupplier

import wx
from product import CDataSupplier
from config import CBriefCode

def create(parent):
    return DlgEditorSupplier(parent)

[wxID_DLGEDITORSUPPLIER, wxID_DLGEDITORSUPPLIERBTNCANCEL, 
 wxID_DLGEDITORSUPPLIERBTNSAVE, wxID_DLGEDITORSUPPLIERTADDR, 
 wxID_DLGEDITORSUPPLIERTBRIEFCODE, wxID_DLGEDITORSUPPLIERTCADDR, 
 wxID_DLGEDITORSUPPLIERTCBRIEFCODE, wxID_DLGEDITORSUPPLIERTCDEBT, 
 wxID_DLGEDITORSUPPLIERTCEMAIL, wxID_DLGEDITORSUPPLIERTCFAX, 
 wxID_DLGEDITORSUPPLIERTCLINKMAN, wxID_DLGEDITORSUPPLIERTCLMPHONE, 
 wxID_DLGEDITORSUPPLIERTCMEMO, wxID_DLGEDITORSUPPLIERTCNAME, 
 wxID_DLGEDITORSUPPLIERTCPHONE, wxID_DLGEDITORSUPPLIERTDEBT, 
 wxID_DLGEDITORSUPPLIERTDEBTMEMO, wxID_DLGEDITORSUPPLIERTEMAIL, 
 wxID_DLGEDITORSUPPLIERTFAX, wxID_DLGEDITORSUPPLIERTLINKMAN, 
 wxID_DLGEDITORSUPPLIERTLMPHONE, wxID_DLGEDITORSUPPLIERTMEMO, 
 wxID_DLGEDITORSUPPLIERTNAME, wxID_DLGEDITORSUPPLIERTPHONE, 
] = [wx.NewId() for _init_ctrls in range(24)]

class DlgEditorSupplier(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGEDITORSUPPLIER,
              name=u'DlgEditorSupplier', parent=prnt, pos=wx.Point(378, 236),
              size=wx.Size(463, 436), style=wx.DEFAULT_DIALOG_STYLE,
              title=u'\u7f16\u8f91\u4f9b\u5e94\u5546')
        self.SetClientSize(wx.Size(455, 402))

        self.TName = wx.StaticText(id=wxID_DLGEDITORSUPPLIERTNAME,
              label=u'\u5355\u4f4d\u540d\u79f0\uff1a', name=u'TName',
              parent=self, pos=wx.Point(32, 48), size=wx.Size(60, 14), style=0)

        self.TCName = wx.TextCtrl(id=wxID_DLGEDITORSUPPLIERTCNAME,
              name=u'TCName', parent=self, pos=wx.Point(104, 40),
              size=wx.Size(100, 22), style=0, value=u'')
        self.TCName.Bind(wx.EVT_TEXT, self.OnTCNameText,
              id=wxID_DLGEDITORSUPPLIERTCNAME)

        self.TBriefCode = wx.StaticText(id=wxID_DLGEDITORSUPPLIERTBRIEFCODE,
              label=u'\u52a9\u8bb0\u7801\uff1a', name=u'TBriefCode',
              parent=self, pos=wx.Point(240, 48), size=wx.Size(48, 14),
              style=0)

        self.TCBriefCode = wx.TextCtrl(id=wxID_DLGEDITORSUPPLIERTCBRIEFCODE,
              name=u'TCBriefCode', parent=self, pos=wx.Point(312, 40),
              size=wx.Size(100, 22), style=0, value=u'')

        self.TCEmail = wx.TextCtrl(id=wxID_DLGEDITORSUPPLIERTCEMAIL,
              name=u'TCEmail', parent=self, pos=wx.Point(312, 80),
              size=wx.Size(100, 22), style=0, value=u'')
        self.TCEmail.SetAutoLayout(False)

        self.TEmail = wx.StaticText(id=wxID_DLGEDITORSUPPLIERTEMAIL,
              label=u'\u7535\u5b50\u90ae\u4ef6\uff1a', name=u'TEmail',
              parent=self, pos=wx.Point(240, 88), size=wx.Size(60, 14),
              style=0)

        self.TCLinkman = wx.TextCtrl(id=wxID_DLGEDITORSUPPLIERTCLINKMAN,
              name=u'TCLinkman', parent=self, pos=wx.Point(104, 160),
              size=wx.Size(100, 22), style=0, value=u'')

        self.TLinkman = wx.StaticText(id=wxID_DLGEDITORSUPPLIERTLINKMAN,
              label=u'\u8054\u7cfb\u4eba\uff1a', name=u'TLinkman', parent=self,
              pos=wx.Point(32, 168), size=wx.Size(48, 14), style=0)

        self.TAddr = wx.StaticText(id=wxID_DLGEDITORSUPPLIERTADDR,
              label=u'\u8054\u7cfb\u5730\u5740\uff1a', name=u'TAddr',
              parent=self, pos=wx.Point(32, 128), size=wx.Size(60, 14),
              style=0)

        self.TCAddr = wx.TextCtrl(id=wxID_DLGEDITORSUPPLIERTCADDR,
              name=u'TCAddr', parent=self, pos=wx.Point(104, 120),
              size=wx.Size(100, 22), style=0, value=u'')

        self.TFax = wx.StaticText(id=wxID_DLGEDITORSUPPLIERTFAX,
              label=u'\u4f20      \u771f\uff1a', name=u'TFax', parent=self,
              pos=wx.Point(240, 128), size=wx.Size(60, 14), style=0)

        self.TCFax = wx.TextCtrl(id=wxID_DLGEDITORSUPPLIERTCFAX, name=u'TCFax',
              parent=self, pos=wx.Point(312, 120), size=wx.Size(100, 22),
              style=0, value=u'')

        self.TCPhone = wx.TextCtrl(id=wxID_DLGEDITORSUPPLIERTCPHONE,
              name=u'TCPhone', parent=self, pos=wx.Point(104, 80),
              size=wx.Size(100, 22), style=0, value=u'')

        self.TPhone = wx.StaticText(id=wxID_DLGEDITORSUPPLIERTPHONE,
              label=u'\u516c\u53f8\u7535\u8bdd\uff1a', name=u'TPhone',
              parent=self, pos=wx.Point(32, 88), size=wx.Size(60, 14), style=0)

        self.TMemo = wx.StaticText(id=wxID_DLGEDITORSUPPLIERTMEMO,
              label=u'\u5907      \u6ce8\uff1a', name=u'TMemo', parent=self,
              pos=wx.Point(32, 200), size=wx.Size(60, 14), style=0)

        self.TCMemo = wx.TextCtrl(id=wxID_DLGEDITORSUPPLIERTCMEMO,
              name=u'TCMemo', parent=self, pos=wx.Point(104, 208),
              size=wx.Size(288, 56), style=wx.TE_MULTILINE, value=u'')

        self.TDebt = wx.StaticText(id=wxID_DLGEDITORSUPPLIERTDEBT,
              label=u'\u671f\u521d\u5e94\u6536\uff1a', name=u'TDebt',
              parent=self, pos=wx.Point(32, 280), size=wx.Size(60, 14),
              style=0)

        self.TCDebt = wx.TextCtrl(id=wxID_DLGEDITORSUPPLIERTCDEBT,
              name=u'TCDebt', parent=self, pos=wx.Point(104, 280),
              size=wx.Size(100, 22), style=0, value=u'0')

        self.TDebtMemo = wx.StaticText(id=wxID_DLGEDITORSUPPLIERTDEBTMEMO,
              label=u'\uff08\u65b0\u589e\u5ba2\u6237\u65f6\u6b20\u6b3e\u91d1\u989d\uff0c\u4e0e\u8ba2\u5355\u6b20\u6b3e\u7d2f\u52a0\uff09',
              name=u'TDebtMemo', parent=self, pos=wx.Point(208, 288),
              size=wx.Size(228, 14), style=0)

        self.btnSave = wx.Button(id=wxID_DLGEDITORSUPPLIERBTNSAVE,
              label=u'\u4fdd\u5b58', name=u'btnSave', parent=self,
              pos=wx.Point(88, 336), size=wx.Size(75, 24), style=0)
        self.btnSave.SetDefault()
        self.btnSave.Bind(wx.EVT_BUTTON, self.OnBtnSaveButton,
              id=wxID_DLGEDITORSUPPLIERBTNSAVE)

        self.btnCancel = wx.Button(id=wxID_DLGEDITORSUPPLIERBTNCANCEL,
              label=u'\u53d6\u6d88', name=u'btnCancel', parent=self,
              pos=wx.Point(248, 336), size=wx.Size(75, 24), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGEDITORSUPPLIERBTNCANCEL)

        self.TLmPhone = wx.StaticText(id=wxID_DLGEDITORSUPPLIERTLMPHONE,
              label=u'\u8054\u7cfb\u4eba\u7535\u8bdd\uff1a', name=u'TLmPhone',
              parent=self, pos=wx.Point(240, 168), size=wx.Size(72, 14),
              style=0)

        self.TCLmPhone = wx.TextCtrl(id=wxID_DLGEDITORSUPPLIERTCLMPHONE,
              name=u'TCLmPhone', parent=self, pos=wx.Point(312, 160),
              size=wx.Size(100, 22), style=0, value=u'')

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.myInit()
        
    def myInit(self):
        self.curObj = None

    def setEditObj(self, obj):
        self.curObj = obj
        
        self.TCName.SetValue(obj.getValue(obj.COL_NAME))
        self.TCBriefCode.SetValue(obj.getValue(obj.COL_BREIFCODE))
        self.TCPhone.SetValue(obj.getValue(obj.COL_PHONE))
        self.TCEmail.SetValue(obj.getValue(obj.COL_EMAIL))
        self.TCAddr.SetValue(obj.getValue(obj.COL_ADDR))
        self.TCFax.SetValue(obj.getValue(obj.COL_FAX))
        self.TCLinkman.SetValue(obj.getValue(obj.COL_LINKMAN))
        self.TCLmPhone.SetValue(obj.getValue(obj.COL_LMPHONE))
        self.TCMemo.SetValue(obj.getValue(obj.COL_MEMO))
        self.TCDebt.SetValue(str(obj.getValue(obj.COL_DEBT)))
        
    def OnTCNameText(self, event):
        name = self.TCName.GetValue()
        code = CBriefCode.getBriefCode(name)
        self.TCBriefCode.SetValue(code)

    def OnBtnSaveButton(self, event):
        if (self.TCName.GetValue() == ''):
            dlg = wx.MessageDialog(self, u'必须设置单位姓名', u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        cls = CDataSupplier
        data = {cls.COL_NAME:self.TCName.GetValue(),
                cls.COL_BREIFCODE:self.TCBriefCode.GetValue(),
                cls.COL_PHONE:self.TCPhone.GetValue(),
                cls.COL_ADDR:self.TCAddr.GetValue(),
                cls.COL_EMAIL:self.TCEmail.GetValue(),
                cls.COL_LMPHONE:self.TCLmPhone.GetValue(),
                cls.COL_FAX:self.TCFax.GetValue(),
                cls.COL_MEMO:self.TCMemo.GetValue(),
                cls.COL_LINKMAN:self.TCLinkman.GetValue(),
                cls.COL_DEBT:float(self.TCDebt.GetValue())}
        
        if (self.curObj is not None):
            self.curObj.setMulFieldVal(data)
            self.curObj.update()
        else:
            obj = cls.factory()
            #obj.assignNewId()
            obj.setMulFieldVal(data)
            obj.insert()
            self.curObj = obj
            
        #self.SetReturnCode(wx.ID_OK)
        self.EndModal(wx.ID_OK)

    def OnBtnCancelButton(self, event):
        self.EndModal(wx.ID_CANCEL)
